package com.tutu.sys.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.tutu.common.GlobalConstants;
import com.tutu.sys.dao.SysMsgInstationMapper;
import com.tutu.sys.dao.SysMsgUserMapper;
import com.tutu.sys.entity.SysMsgInstation;
import com.tutu.sys.entity.SysMsgUser;
import com.tutu.sys.entity.SysMsgUserExample;
import com.tutu.sys.service.ISysMsgService;

@Service
public class SysMsgServiceImpl implements ISysMsgService {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private SysMsgUserMapper sysMsgUserMapper;
	@Resource
	private SysMsgInstationMapper sysMsgInstationMapper;
	
	@Override
	public List<SysMsgUser> getMyMsgs(SysMsgUser sysMsgUser) {
		return this.sysMsgUserMapper.getMyMsgs(sysMsgUser);
	}

	@Override
	public List<SysMsgInstation> getMySendMsg(SysMsgInstation sysMsgInstation,Page<SysMsgInstation> page) {
		long total = this.sysMsgInstationMapper.getMySendMsgCount(sysMsgInstation);
		page.setTotal(total);
		List<SysMsgInstation> sysMsgInstations = this.sysMsgInstationMapper.getMySendMsgList(sysMsgInstation, page);
		page.addAll(sysMsgInstations);
		return page;
	}

	@Override
	public void saveSysMsg(SysMsgInstation sysMsgInstation, Collection<String> receivers) {
		if(CollectionUtils.isEmpty(receivers)){
			return;
		}
		this.sysMsgInstationMapper.insert(sysMsgInstation);
		
		for(String receiver : receivers){
			SysMsgUser sysMsgUser = new SysMsgUser();
			sysMsgUser.setMsgId(sysMsgInstation.getMsgId());
			sysMsgUser.setReceiver(receiver);
			sysMsgUser.setAliveFlag(GlobalConstants.FLAG_ENABLE);
			sysMsgUser.setCreator(sysMsgInstation.getCreator());
			sysMsgUser.setCreateTime(sysMsgInstation.getCreateTime());
			this.sysMsgUserMapper.insert(sysMsgUser);
		}
	}

	@Override
	public SysMsgUser getSysMsgUser(String userMsgId) {
		SysMsgUser sysMsgUser = new SysMsgUser();
		sysMsgUser.setUserMsgId(userMsgId);
		List<SysMsgUser> sysMsgUsers = this.sysMsgUserMapper.getMyMsgs(sysMsgUser);
		if(CollectionUtils.isNotEmpty(sysMsgUsers)){
			return sysMsgUsers.get(0);
		}
		return null;
	}

	@Override
	public void updateSysMsgUser(SysMsgUser sysMsgUser) {
		this.sysMsgUserMapper.updateByPrimaryKeySelective(sysMsgUser);
	}

	@Override
	public SysMsgInstation getSysMsgInstationById(String msgId) {
		return this.sysMsgInstationMapper.selectByPrimaryKey(msgId);
	}

	@Override
	public int getMyMsgCount(SysMsgUser sysMsgUser) {
		SysMsgUserExample sysMsgUserExample = new SysMsgUserExample();
		com.tutu.sys.entity.SysMsgUserExample.Criteria criteria = sysMsgUserExample.createCriteria();
		if(StringUtils.isNotBlank(sysMsgUser.getAliveFlag()))
			criteria.andAliveFlagEqualTo(sysMsgUser.getAliveFlag());
		if(StringUtils.isNotBlank(sysMsgUser.getReceiver()))
			criteria.andReceiverEqualTo(sysMsgUser.getReceiver());
		criteria.andViewDateIsNull();
		return this.sysMsgUserMapper.countByExample(sysMsgUserExample);
	}
	
}
